<template>
  <div
    ref="videoPlayer"
    class="video-player-box vjs-custom-skin"
    :playsinline="playsinline"
    @play="onPlayerPlay($event)"
    @pause="onPlayerPause($event)"
    @ended="onPlayerEnded($event)"
    @loadeddata="onPlayerLoadeddata($event)"
    @waiting="onPlayerWaiting($event)"
    @playing="onPlayerPlaying($event)"
    @timeupdate="onPlayerTimeupdate($event)"
    @canplay="onPlayerCanplay($event)"
    @canplaythrough="onPlayerCanplaythrough($event)"
    @ready="playerReadied"
    @statechanged="playerStateChanged($event)"
    v-video-player:myVideoPlayer="playerOptions"
  ></div>
</template>
<script>
import "video.js/dist/video-js.css";
import "vue-video-player/src/custom-theme.css";
export default {
  props: {
    VideoSrc: {
      type: String,
      default: "",
    },
    aspectRatio: {
      type: String,
      default: "642:292",
    },
    autoplay: {
      type: Boolean,
      default: false,
    },
    controls: {
      type: Boolean,
      default: false,
    },
    muted: {
      type: Boolean,
      default: true,
    },
    loop: {
      type: Boolean,
      default: false,
    },

  },
  components: {},
  data() {
    return {
      playsinline: true,
      playerOptions: {
        autoplay: this.autoplay, // 自动播放
        controls: this.controls, // 是否显示控制栏
        loop: this.loop,
        muted: this.muted, // 是否静音
        preload: "auto", // 视频预加载
        language: "zh-CN",
        techOrder: ["html5", "flash"], // 兼容顺序
        flash: { hls: { withCredentials: false } },
        html5: { hls: { withCredentials: false } },
        aspectRatio: this.aspectRatio,
        playbackRates: [0.7, 1.0, 1.5, 2.0], //倍速
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [
          {
            type: "video/flv",
            type: "video/ogg",
            type: "video/webm",
            type: "video/mp4",
            src: this.VideoSrc,
            // src: "https://cdn.theguardian.tv/webM/2015/07/20/150716YesMen_synd_768k_vp8.webm",
          },
        ],
        notSupportedMessage: "此视频暂无法播放，请稍后再试",
      },
    };
  },
  computed: {
    player() {
      return this.$refs.videoPlayer.player;
    },
  },
  watch: {
    VideoSrc: function (val) {
      let playerOptions=JSON.parse(JSON.stringify(this.playerOptions))
      playerOptions.sources[0].src=val
      this.playerOptions=playerOptions
    },
  },
  methods: {
    // listen event
    onPlayerPlay(player) {
      // console.log("player play!", player);
    },
    onPlayerPause(player) {
      //console.log("player pause!", player);
    },
    onPlayerEnded(player) {
      //console.log("player ended!", player);
    },
    onPlayerLoadeddata(player) {
      //console.log("player Loadeddata!", player);
    },
    onPlayerWaiting(player) {
      //console.log("player Waiting!", player);
    },
    onPlayerPlaying(player) {
      //console.log("player Playing!", player);
    },
    onPlayerTimeupdate(player) {
      //console.log("player Timeupdate!", player.currentTime());
    },
    onPlayerCanplay(player) {
      //console.log("player Canplay!", player);
    },
    onPlayerCanplaythrough(player) {
      //console.log("player Canplaythrough!", player);
    },
    // or listen state event
    playerStateChanged(playerCurrentState) {
      //console.log("player current update state", playerCurrentState);
    },
    // player is ready
    playerReadied(player) {
      //console.log("example 01: the player is readied", player);
    },
  },
};
</script>

<style scoped lang='scss'>
.video-player-box {
  width: 100%;
  height: 100%;
}
</style>